import * as instockService from '../../services/instock';
import {getJoberNum} from '../../services/shelfDown';
import {queryAssetType} from '../../services/assetType';
import {qryDepts,qryAllUser} from '../../services/user';
import {queryAssetBrand,queryAssetModelAll} from '../../services/assetModel';
import {queryRoomList} from '../../services/tRoom';

export default {
  namespace: 'instock',
  state: {
    list: [],
    assetTypeTree:[],
    roomList:[],
    assetBrandList:[],
    assetModelList:[],
    userList:[],
    assetList:[],
    current : 1,
    currentSize : 10,
    total : 0,
    modalVisible : false,
    modalType : 'create',
    newKey : '',
    newKey1:'',
    detailList:[],
    currentItem: {},
    viewModalVisible:false,
    viewItem:{},
    viewDetailList:[],
    pullOffId:"",
    instockItem:{},
    secondModalVisible: false,
    editModalVisible: false,
    assetCurrent:1,
    assetCurrentSize : 10,
    assetTotal : 0,
    assetItem:{},
    addTimeModalVisible:false,
  },
  reducers: {
    updateState (state, { payload }) {
      return { ...state, ...payload}
    },
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    queryListByParamSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    changeDetailList(state, { payload }) {
      return { ...state,  ...payload, secondModalVisible: false };
    },
    queryBrandListSuc(state, { payload: assetBrandList }) {
      return { ...state, assetBrandList };
    },
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },
    queryUserSuc(state, { payload:userList}) {
      return { ...state, userList };
    },
    queryAssetTypesSuc(state, { payload: assetTypeTree }) {
      return { ...state, assetTypeTree };
    },
    getJoberNumSuc(state, { payload}) {
      return { ...state, pullOffId:payload };
    },
    showModal (state, { payload }) {
      return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false, detailList:[], newKey : new Date().getTime() + '' }
    },
    showViewModal (state, { payload }) {
      return { ...state, ...payload, viewModalVisible: true }
    },
    hideViewModal (state) {
      return { ...state, viewModalVisible: false, }
    },
    showSecondModal (state, { payload }) {
      return { ...state, ...payload, secondModalVisible: true }
    },
    hideSecondModal (state) {
      return { ...state,secondModalVisible: false }
    },
    showEditModal (state, { payload }) {
      return { ...state, ...payload,  editModalVisible: true }
    },
    hideEditModal (state) {
      return { ...state, editModalVisible: false}
    },

  },
  effects: {
    *queryUser({ payload },{ call, put }) {
      const { body }  = yield call(qryAllUser);
      yield put({ type: 'queryUserSuc', payload: body.data });
    },
    *queryAssetType({ payload },{ call, put }) {
      const { body }  = yield call(queryAssetType);
      yield put({ type: 'queryAssetTypesSuc', payload: body.data });
    },
    *queryRoom({ payload },{ call, put }) {
      const { body } = yield call(queryRoomList,payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    },
    *queryAssetBrand({ payload },{ call, put }) {
      const { body } = yield call(queryAssetBrand,payload);
      yield put({ type: 'queryBrandListSuc', payload: body.data });
    },
    *queryAssetModel({ payload },{ call, put }) {
      const { body } = yield call(queryAssetModelAll,payload);
      yield put({ type: 'updateState', payload: {assetModelList:body.data} });
    },
    *queryList({ payload },{ call, put }) {
      const { body } = yield call(instockService.queryList,payload);
      yield put({ type: 'queryListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
    },
    *qryById({ payload },{ call, put }){
      const { body } = yield call(getJoberNum,payload);
      yield put({ type: 'getJoberNumSuc', payload: body.data, });
      yield put({type: 'showModal'});
      yield put({type: 'queryAssetType'});
      yield put({ type: 'queryAssetBrand', payload: {  },});
    },
    *queryAssetList({ payload }, { call, put }) {
      yield put({ type: 'querAssetListByParam', payload });
      yield put({ type: 'showSecondModal', payload: { assetItem: payload} });
    },
    *onViewItem({ payload },{ call, put }){
      const { body } = yield call(instockService.qryById,payload);
      yield put({type: 'showViewModal', payload: {viewItem: body.data, viewDetailList:body.dataVo},
      });
    },
    *onConfirmInstock({ payload },{ call, put }){
      const { body } = yield call(instockService.qryById,payload);
      yield put({type: 'showEditModal', payload: {viewItem: body.data, viewDetailList:body.dataVo},});
    },

    *instockUpdateStatus({ payload ,search },{ call, put }){
      const { body } = yield call(instockService.updateStatus,payload);
      yield put({ type: 'queryList', payload:search });   
    },

    *querAssetListByParam({ payload },{ call, put }){
      const { body } = yield call(instockService.querAssetListByParam,payload);
      yield put({ type: 'queryListByParamSuc', payload: { assetList:body.data,assetCurrent:body.current, assetCurrentSize:body.size, assetTotal:body.iTotalRecords}});
    },
    *create({ payload },{ call, put }){
      const { body } = yield call(instockService.create,payload);
      yield put({ type: 'hideModal' });
      yield put({ type: 'reload' });
    },
    *confirmInstock({ payload ,search},{ call, put }){
      const { body } = yield call(instockService.confirmInstock,payload);
      yield put({ type: 'hideEditModal' });
      yield put({ type: 'queryList', payload:search  });
    },

    *showInstockTime({ payload }, { call, put }) {
      yield put({ type: 'updateState', payload: { instockItem: payload,addTimeModalVisible: true,newKey1 : new Date().getTime() + ''} });
    },
    *remove({ payload,search },{ call, put }){
      const { body } = yield call(instockService.remove,payload);
      yield put({ type: 'queryList', payload:search });
    },
    *reload(action, { put }) {
      yield put({ type: 'queryList', payload:{ 'name':'' }});
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/instock') {
          dispatch({ type: 'queryUser' });
          dispatch({ type: 'queryRoom' });
          dispatch({ type: 'queryList', payload:{ 'status':'' }});
        }
      });
    },
  },
};
